package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zy.common.Result;
import com.zy.entity.AddressBook;
import com.zy.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author xyz
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    @ApiOperation(value = "新增地址簿接口")
    public Result<AddressBook> save(HttpServletRequest httpServletRequest, @RequestBody AddressBook addressBook) {
        Long userId = (Long) httpServletRequest.getSession().getAttribute("user");
        /*
         * 这里留了个坑，我把数据库create_Id和更新时间等字段改成了非空了，算是遗留Bug
         * 不知道为什么BaseContext拿不到用户Id数据，所以拿不到create_Id
         *
         * */
        addressBook.setUserId(userId);
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    @ApiOperation(value = "设置默认地址接口")
    public Result<AddressBook> setDefault(HttpServletRequest httpServletRequest, @RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        Long userId = (Long) httpServletRequest.getSession().getAttribute("user");
        wrapper.eq(AddressBook::getUserId, userId);
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @ApiOperation(value = "根据id查询地址接口")
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(addressBook);
        } else {
            return Result.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @ApiOperation(value = "查询默认地址接口")
    @GetMapping("default")
    public Result<AddressBook> getDefault(HttpServletRequest httpServletRequest) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = (Long) httpServletRequest.getSession().getAttribute("user");
        queryWrapper.eq(AddressBook::getUserId, userId);
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @ApiOperation(value = "查询指定用户的全部地址接口")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(HttpServletRequest httpServletRequest, AddressBook addressBook) {


        Long userId = (Long) httpServletRequest.getSession().getAttribute("user");
        addressBook.setUserId(userId);
        log.info("addressBook:{}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.eq(AddressBook::getIsDeleted,0);
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return Result.success(addressBookService.list(queryWrapper));
    }

    /**
     * 修改用户地址
     */
    @ApiOperation(value = "修改用户地址")
    @PutMapping
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("更新地址簿{}", addressBook);
        addressBookService.updateById(addressBook);
        return Result.success("地址簿更新完成");
    }

    /**
     * 批量删除用户地址
     */
    @ApiOperation(value = "批量删除用户地址")
    @DeleteMapping
    public Result<String> delAddressBook(Long ids) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(ids);
        addressBook.setIsDeleted(1);
        addressBookService.updateById(addressBook);
        return Result.success("地址删除成功");
    }
}
